﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AutoMapper;
using EasyMvcBlog.DTO;
using EasyMvcBlog.Data;
using EasyMvcBlog.Infrastructure.Repository;

namespace EasyMvcBlog.Domain
{
    public class TagDomain
    {
        private RepositoryBase<Tag> tagRepository = new RepositoryBase<Tag>();

        /// <summary>
        /// 所有的tag标签
        /// </summary>
        public List<TagDTO> AllTags
        {
            get
            {
                List<Tag> tags = tagRepository.GetAll();
                Mapper.CreateMap<Tag, TagDTO>();
                return Mapper.Map<List<Tag>, List<TagDTO>>(tags);
            }
        }

        public List<TagDTO> GetTagsByPage(int pageIndex, int pageSize)
        {
            IList<Tag> tags = tagRepository.GetPage(pageIndex, pageSize, null, true, null);
            Mapper.CreateMap<Tag, TagDTO>();
            return Mapper.Map<IList<Tag>, List<TagDTO>>(tags);
        }

        public bool DeleteTag(int tagId)
        {
            if (tagId > 0)
            {
                //删除所有文章中包含这个标签的信息
                ArticleDomain articleDomain = new ArticleDomain();
                List<ArticleDTO> articles = articleDomain.GetArticleByTag(tagId);

                articleDomain.RemoveArticlesTag(articles, tagId);

                tagRepository.DeleteById<Tag>(tagId);
                return true;
            }
            return false;
        }

        public void AddTags(List<Tag> newTags)
        {
            foreach (Tag tag in newTags)
            {
                tagRepository.Add(tag);
            }
        }
    }
}
